package com.gs.cd.service.old.controller.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gs.cd.model.resource.BaseEntity;
import com.gs.cd.model.resource.SysResourceGroup;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author hippo
 * @since 2020-02-12
 */
@Data
public class ResourceGroup extends BaseEntity {


    private static final long serialVersionUID = 5917655380675290053L;
    private String id;

    private String parentId;
    /**
     * 租户Id
     */
    private String tenantCode;

    /**
     * 资源组名
     */
    private String name;
    /**
     * code
     */
    private String code;

    /**
     * 描述
     */
    private String description;
    /**
     * 排序
     */
    private Integer sort;


    /**
     * 资源组所属层级 tenant,system
     */
    private String category;

    /**
     * 子资源组
     */
    private List<ResourceGroup> children = new ArrayList<>();


    @JsonProperty("resource_ids")
    List<String> resourceList = new ArrayList<>();

    public String getTenantCode() {
        return "default";
    }

    public void setTenantCode(String tenantCode) {
        this.tenantCode = "default";
    }

    //auth: seven
    //
    //date: 2020/12/25 16:10:15
    public static List<ResourceGroup> parse(@NonNull List<SysResourceGroup> sysResourceGroups) {
        ArrayList<ResourceGroup> resourceGroups = new ArrayList<>();
        sysResourceGroups.forEach(s -> {
            ResourceGroup parse = ResourceGroup.parse(s);
            if (s.getChildResourceGroup() != null && !s.getChildResourceGroup().isEmpty()) {
                List<ResourceGroup> parse1 = parse(s.getChildResourceGroup());
                parse.setChildren(parse1);
            }
            resourceGroups.add(parse);
        });
        return resourceGroups;
    }

    public static ResourceGroup parse(SysResourceGroup sysResourceGroup) {
        if (sysResourceGroup == null) return null;
        ResourceGroup resourceGroup = new ResourceGroup();
        BeanUtil.copyProperties(sysResourceGroup, resourceGroup);
        resourceGroup.setCategory(sysResourceGroup.getCategory() == null ? null : sysResourceGroup.getCategory().getName());
        return resourceGroup;
    }
}
